package com.exartisan.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.exartisan.bean.FbComment;
import com.exartisan.bean.Post;
import com.exartisan.thanhdoanhn.R;
import com.loopj.android.image.SmartImageView;

public class ForumDetailAdapter extends BaseAdapter {

	Context context;
	Post post;
	ArrayList<FbComment> fbComments;
	boolean isMoreDetail;
	String wvURL;

	public ForumDetailAdapter(Context context, Post post,
			ArrayList<FbComment> fbComments, boolean isMoreDetail, String wvURL) {
		this.context = context;
		this.post = post;
		this.fbComments = fbComments;
		this.isMoreDetail = isMoreDetail;
		this.wvURL = wvURL;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		if(isMoreDetail)
			return fbComments.size()+2;
		else return fbComments.size()+1;
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup arg2) {
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		View listView;

		listView = new View(context);
		switch (getItemViewType(position)) {
		case 0:
			if (convertView == null) {
				// declare
				listView = inflater.inflate(R.layout.row_detail_post_forum,
						null);
				SmartImageView imgAvatar = (SmartImageView) listView
						.findViewById(R.id.img_avata);
				TextView tvUserName = (TextView) listView
						.findViewById(R.id.tv_userName);
				TextView tvCreatedDate = (TextView) listView
						.findViewById(R.id.tv_date);
				SmartImageView imgDescription = (SmartImageView) listView
						.findViewById(R.id.img_mainIMG);
				TextView tvTitle = (TextView) listView
						.findViewById(R.id.tv_title);
				TextView tvDescription = (TextView) listView
						.findViewById(R.id.tv_desciption);

				// fill data
				imgAvatar
						.setImageUrl("http://k14.vcmedia.vn/k:994cDL3cmLDsEtiKLBokEWLgcC5xLI/Image/2012/12/121229tekfb4-a68b0/trao-luu-avatar-nghieng-dau-tran-ngap-facebook.jpg");
				tvUserName.setText("Nham PD");
				tvCreatedDate.setText(post.getCreateDate());
				imgDescription.setImageUrl(post.getImageURL());
				tvTitle.setText(post.getTitle());
				tvDescription.setText(post.getDesc());
			} else {
				listView = (View) convertView;
			}
			break;
		case 1:
			if (convertView == null) {
				// declare
				listView = inflater.inflate(R.layout.row_webview_forum, null);
				WebView webView = (WebView) listView
						.findViewById(R.id.wbv_contentForum);
				WebSettings webSettings = webView.getSettings();
				webSettings.setJavaScriptEnabled(true);
				webView.loadUrl(wvURL);
			} else {
				listView = (View) convertView;
			}
			break;
		default:
			if (convertView == null) {
				listView = inflater.inflate(R.layout.row_comments_forum, null);
				SmartImageView imgAvatar = (SmartImageView) listView
						.findViewById(R.id.img_avataComment);
				TextView tvUserName = (TextView) listView
						.findViewById(R.id.tv_userNameComment);
				TextView tvCreatedDate = (TextView) listView
						.findViewById(R.id.tv_dateComment);
				TextView tvComment = (TextView) listView
						.findViewById(R.id.tv_commentForum);

				// fill data
				int posTemp = 0;
				if(isMoreDetail== true) posTemp = position - 2;
				else posTemp = position - 1;
				imgAvatar.setImageUrl(fbComments.get(posTemp).getFbUser()
						.getPicture_url());
				tvUserName.setText(fbComments.get(posTemp).getFbUser()
						.getName());
				tvCreatedDate.setText(fbComments.get(posTemp)
						.getCreated_time());
				tvComment.setText(fbComments.get(posTemp).getMessage());
				
			} else {
				listView = (View) convertView;
			}
			break;
		}
		 
		return listView;
	}
	
	@Override
	public int getViewTypeCount() {
	    return 3;
	}

	
	@Override
	public int getItemViewType(int position) {

		if (position == 0)
			return 0;
		else if (position == 1 && isMoreDetail == true)
			return 1;
		else
			return 2;
	}

}
